package com.evaluationsystem.config.securityFilter;

import com.alibaba.fastjson.JSON;
import com.evaluationsystem.system.entity.UserInfo;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class LoginSuccessHandlerFilter extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res, Authentication auth) throws ServletException, IOException {
        Map<String,Object> r = new HashMap<>();
        res.setContentType("application/json;charset=UTF-8");
        UserInfo principal = (UserInfo) auth.getPrincipal();
        r.put("code","200");
        r.put("msg","ok");
        r.put("href","/evaluation/index");
        r.put("data",principal.getUsername());
        res.getWriter().write(JSON.toJSONString(r));
    }

}
